﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateRectStructureWall.h"
#include "GmUiStructureCommandRegister.h"
#include "GcmpActionBase.h"
#include "StructureCmdIds.h"
#include "IModelView.h"
#include "GbmpModelViewTypes.h"
#include "UiCommonDialog.h"
#include "StructureDefaultFamilyNames.h"
#include "StructureInstanceSecondKeys.h"
#include "AddStructureDynamicPannelUtil.h"
#include "IUserTransaction.h"
#include "IUiView.h"
#include "IUiDocument.h"
#include "StructureLinearInstanceCreator.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"
#include "DrawRectElementBehavior.h"
#include "StructureWallInput.h"
#include "GbmpUiPlatformUtils.h"
#include "StructureInstanceCmdUtils.h"
#include "StructureInstanceUtils.h"
#include "LineFamilyPositionCurveSideType.h"
#include "GbmpJoinUtils.h"
#include "GbmpControlIds.h"
#include "IActionInputEventArgs.h"
#include "IGraphicsElementShape.h"
#include "UiDocumentViewUtils.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

#define ACTION_RECT_STRAIGHT_STRUCTURE_WALL_INPUT_RADIO_Y_POSITION     L"gmRectStraightStructureWallRadioYPosition"

using namespace gcmp;

REGISTER_GM_COMMAND(CmdCreateRectStructureWall)

CmdCreateRectStructureWall::CmdCreateRectStructureWall()
    : CmdCreateRectStructureInstance(ID_CMD_CREATE_RECT_STRUCTURE_WALL)
{
}

bool CmdCreateRectStructureWall::OnExecuteCommand()
{
    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    const std::wstring& viewType = pModelView->GetUserDataId();

    if (PlanViewType != viewType && View3DViewType != viewType)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"创建墙"), GBMP_TR(L"此视图下不支持创建墙"), (int)UiCommonDialog::ButtonType::OK);
        return false;
    }

    return true;
}

StructureFamilyUiUtils::FamilyCreators CmdCreateRectStructureWall::GetDefaultFamilyCreators() const
{
    StructureFamilyUiUtils::FamilyCreators defaultFamilyCreators
    {
        { DEFAULT_RECT_WALL_FAMILY_NAME, BuiltInStructureFamilies::CreateBuiltInWallFamilyDocument }
    };

    return defaultFamilyCreators;
}

std::wstring CmdCreateRectStructureWall::GetDefaultFamilyName() const
{
    return DEFAULT_RECT_WALL_FAMILY_NAME;
}

UniIdentity CmdCreateRectStructureWall::GetCategory() const
{
    return BuiltInCategoryUniIdentities::BICU_STRUCTURE_WALL;
}

Int32 CmdCreateRectStructureWall::GetCategorySecondKey() const
{
    return (Int32)StructureWallType::Basic;
}

void CmdCreateRectStructureWall::SetupRibbonContextualPanel()
{
    AddStructureDynamicPannelUtil::SetupWallCreationContext(m_pDrawRectAction, ID_PAGE_GBMP_MODIFY, false);
}

bool CmdCreateRectStructureWall::CreatePresetElement(ElementId& elementId)
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"创建预设参数对象"), true);

    StructureWallInput wallInput;
    wallInput.CreationOption = ElementCreationOptions::Transient;
    wallInput.CanBeShared = false;
    wallInput.Visible = false;
    wallInput.PositionType = ElementInputPositionType::Line;
    std::vector<Vector3d> pts
    {
        Vector3d(-1, 0, 0),
        Vector3d(1, 0, 0),
    };
    wallInput.PositionPoints = pts;

    bool bOk = GetInstanceInput(wallInput);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"GetInstanceInput失败",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IStructureLinearInstanceCreator> opInstanceCreator = GetInstanceCreator();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opInstanceCreator, L"opInstanceCreator为空",L"GDMPLab",L"2024-03-30");

    ElementId presetElementId = opInstanceCreator->CreateInstance(&wallInput, true);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(presetElementId.IsValid(), L"presetElementId无效",L"GDMPLab",L"2024-03-30");

    opTransaction->Commit();

    elementId = presetElementId;
    return true;
}

bool CmdCreateRectStructureWall::OnRectFinish(IUiView* pCurrentView, const Vector3d& firstPt, const Vector3d& cornerPt)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pCurrentView->GetUiDocument()->GetDbDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IModelView* pModelView = pCurrentView->GetModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDoc, GBMP_TR(L"创建墙"));
    {
        std::vector<ElementId> wallElementIds;

        Vector3d rectCorners[4];
        bool bOk = GbmpUiPlatformUtils::GetRectCorners(pModelView, firstPt, cornerPt, rectCorners);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

        for (int i = 0; i < 4; ++i)
        {
            Vector3d startPt = rectCorners[i];
            Vector3d endPt = rectCorners[(i + 1) % 4];
            std::vector<Vector3d> pts
            {
                startPt, endPt
            };
            StructureWallInput wallInput;
            wallInput.PositionPoints = pts;

            bOk = GetInstanceInput(wallInput);
            DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"GetInstanceInput失败",L"GDMPLab",L"2024-03-30");

            OwnerPtr<IStructureLinearInstanceCreator> opInstanceCreator = GetInstanceCreator();
            DBG_WARN_AND_RETURN_FALSE_UNLESS(opInstanceCreator, L"opInstanceCreator为空",L"GDMPLab",L"2024-03-30");

            ElementId wallElementId = opInstanceCreator->CreateInstance(&wallInput, true);
            DBG_WARN_AND_RETURN_FALSE_UNLESS(wallElementId.IsValid(), L"wallElementId无效",L"GDMPLab",L"2024-03-30");

            wallElementIds.push_back(wallElementId);
        }

        FOR_EACH(wallElementId, wallElementIds)
        {
            GbmpJoinUtils::CreateJoinsAndCuts(pDoc, wallElementId);
        }
    }
    ut->Commit();
    return true;
}

bool CmdCreateRectStructureWall::OnRectPreview(IUiView* pCurrentView, const Vector3d& firstPt, const Vector3d& pos, OwnerPtr<IGraphicsElementShape>& opPreviewGraphics)
{
    return false;
}

bool CmdCreateRectStructureWall::GetInstanceInput(StructureWallInput& input) const
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IUiView* pUIView = UiDocumentViewUtils::GetCurrentUiView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pUIView, L"pUIView为空",L"GDMPLab",L"2024-03-30");

    double thickness(0);
    bool bOk = StructureInstanceUtils::GetWallCoreThickness(pDoc, m_instanceTypeId, thickness);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"GetBeamThickness失败",L"GDMPLab",L"2024-03-30");

    DBG_WARN_AND_RETURN_FALSE_UNLESS(!input.PositionPoints.empty(), L"points无效",L"GDMPLab",L"2024-03-30");
    std::vector<Vector3d> positioningPoints = StructureInstanceUtils::GetPositioningPoints(input.PositionPoints, thickness, m_yPositioningType);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(!positioningPoints.empty(), L"GetPositioningPoints失败",L"GDMPLab",L"2024-03-30");
    input.PositionPoints = positioningPoints;

    bOk = StructureInstanceCmdUtils::GenerateBasicWallInput(pDoc, pUIView->GetModelView(),
        this->GetCategory(), this->GetCategorySecondKey(), m_familyId, m_instanceTypeId, input);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"GenerateStraightBasicWallInput失败",L"GDMPLab",L"2024-03-30");

    return true;
}

OwnerPtr<IStructureLinearInstanceCreator> CmdCreateRectStructureWall::GetInstanceCreator() const
{
    return NEW_AS_OWNER_PTR(StructureStraightBasicWallCreator);
}

OwnerPtr<DrawRectElementBehavior> CmdCreateRectStructureWall::CreateDrawRectElementBehavior()
{
    OwnerPtr<DrawRectElementBehavior> opBehavior = NEW_AS_OWNER_PTR(DrawRectElementBehavior, UiDocumentViewUtils::GetCurrentDocument());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opBehavior, L"opBehavior为空",L"GDMPLab",L"2024-03-30");
    opBehavior->SetOnFinishCallback(DrawRectElementBehavior::CreateOnFinishCallback(&CmdCreateRectStructureWall::OnRectFinish, this));
    opBehavior->SetOnPreviewCallback(DrawRectElementBehavior::CreateOnPreviewCallback(&CmdCreateRectStructureWall::OnRectPreview, this));

    return TransferOwnership(opBehavior);
}

bool CmdCreateRectStructureWall::DefineInputDialog(DefineInputDialogParams& inputItems)
{
    // 对话框标题
    inputItems.title = GBMP_TR(L"结构墙");
    // 定义item选项, 默认选中 “中心”
    m_yPositioningType = LineFamilyPositionCurveSideType::CenterLineFamilyPositionCurveSideType;
    StructureInstanceCmdUtils::AddYPositionInputItemsToDialogNew(ACTION_RECT_STRAIGHT_STRUCTURE_WALL_INPUT_RADIO_Y_POSITION,
        m_yPositioningType, inputItems.inputItems);

    return true;
}

bool CmdCreateRectStructureWall::OnActionInputEvent(IActionInputEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pArgs, L"pArgs为空",L"GDMPLab",L"2024-03-30");
    const std::pair<std::wstring, gcmp::Any>& msg = pArgs->GetInputMessage();
    const std::wstring& eventID = msg.first;
    gcmp::Any inputValue = msg.second;
    if (eventID == ACTION_RECT_STRAIGHT_STRUCTURE_WALL_INPUT_RADIO_Y_POSITION)
    {
        StructureInstanceCmdUtils::GetYPositionFromInputEventNew(inputValue.cast<std::wstring>(), m_yPositioningType);
    }

    return true;
}
